//***************************医生操作页面  start***************************

//添加预约 方块 点击  start
$('.userStatistics .addReservationBox').click(function(){
    window.location.href = $(this).attr('data-url');
});
//添加预约 方块 点击  end

//医生统计 查看预约信息  start
$('#statistical .viewReservation').click(function(){
    $('#adminViewReservation').modal('show');
    var doctor_id=$('#reservationList .searchBox select[name="doctor"]').val();
    var date=$('.statisticsChartBox span.chartTitleData').html();
    var result={name:'',phone:'',time:date,status:'2',doctor_id:doctor_id,page:1};
    var period=$(this).attr('data-time');
    if(period === 'am'){ result.am=1; }else if (period === 'pm'){ result.am=0; }
    getModalReservationMessage(result);
});
//医生统计 查看预约信息  end

//日历点击  start
$('#calendar').delegate('tbody tr td','click',function(){
    if(!($(this).hasClass('disabled'))){
        var date=$(this).attr('data');
        date=date.substr(0,4)+'-'+date.substr(4,2)+'-'+date.substr(6,2);
        getChartData(date);
    }
});
//日历点击  end

//根据日期 获取统计图表信息和上下午人数信息  start
function getChartData(date){
    ajaxGet(RESERVATIONCENTERURL.statistical.doctor.statistics, {time:date}, function(data){
        if(data.code === 10000){
            $('.statisticsChartBox span.chartTitleData').html(date);
            $('#statisticalNumber .morning span').html(((data.data.am/data.data.total)*100).toFixed(0));
            $('#statisticalNumber .morningRightNumber p span').html(data.data.am);
            $('#statisticalNumber .afternoon span').html(((data.data.pm/data.data.total)*100).toFixed(0));
            $('#statisticalNumber .afternoonRightNumber p span').html(data.data.pm);
            $('#statisticalNumber .allRightNumber p span').html(data.data.total);
            var xAxisData=[],seriesData=[];
            $.each(data.data.section,function(i,v){
                xAxisData.push(i);
                seriesData.push(v);
            });
            statisticalChart('chartArea',xAxisData,seriesData);
            $('#reservationCenterNav li[data-page="statistical"]').attr('data-load','yes');//改变标签栏状态
        }else if(data.code === 40100){
            aClick();
        }else if(data.code === '40001'){
            $('.statisticsChartBox span.chartTitleData').html(date);
            $('#statisticalNumber .morning span').html(0);
            $('#statisticalNumber .morningRightNumber p span').html(0);
            $('#statisticalNumber .afternoon span').html(0);
            $('#statisticalNumber .afternoonRightNumber p span').html(0);
            $('#statisticalNumber .allRightNumber p span').html(0);
            $('#chartArea').html('<img src="../images/chartZero.png">');
        }else{
            alertModel('','获取失败，'+data.message);
        }
    }, function(){ });
};
//根据日期 获取统计图表信息和上下午人数信息  end


//***************************医生操作页面  end***************************




//***************************管理员操作页面  start***************************

//选择查看日期 输入框失去焦点 自动补齐另一个输入框并计算出表格日期  start
$('.adminStatistics .adminStatisticsDate .periodTime input').blur(function(){
    var _this=$(this);
    var siblingInput;
    setTimeout(function(){
        if(_this.val()){
            if(_this.attr('name')==='start_time'){
                siblingInput=_this.next().next();
                siblingInput.val(addDate(_this.val(),6));
            }else{
                siblingInput=_this.prev().prev();
                siblingInput.val(addDate(_this.val(),-6));
            }
        }
    },150);
});
//选择查看日期 输入框失去焦点 自动补齐另一个输入框并计算出表格日期  end

//查询按钮 请求表格数据  start
$('#adminStatisticsSearchBtn').click(function(){
    var sTimeInput=$('.adminStatistics .adminStatisticsDate input[name="start_time"]');
    var eTimeInput=$('.adminStatistics .adminStatisticsDate input[name="end_time"]');
    if(!(sTimeInput.val()) || !(eTimeInput.val())){
        sTimeInput.val(formatDate(new Date()));
        eTimeInput.val(addDate(formatDate(new Date()),6));
    }else if((sTimeInput.val()) && (eTimeInput.val())){
        if(addDate(sTimeInput.val(),6) != eTimeInput.val()){
            eTimeInput.val(addDate(sTimeInput.val(),6));
        }
    }
    var statisticsDataEl=$(this).parent().parent().find('.form-control');
    var result={};
    for(var i=0 ;i<statisticsDataEl.length;i++){
        result[$(statisticsDataEl[i]).attr('name')]=$(statisticsDataEl[i]).val();
    }
    getVisitsStatistical(result);
});
//查询按钮 请求表格数据  end

// 根据查看日期 获取医生出诊情况统计  start
function getVisitsStatistical(result){
    var statusArr=['stop','normal','full','detonation'];
    ajaxGet(RESERVATIONCENTERURL.statistical.admin.visit, result, function(data){
        if(data.code === 10000){
            var str='<thead><tr><th></th>',amStr='',pmStr='';
            $.each(data.data,function(i,v){
                str+= '<th>'+getWeek(i)+'<span>'+i+'</span></th>';
            });
            str+='</tr></thead> <tbody>';
            amStr+='<tr><td>上午</td>';
            pmStr+='<tr><td>下午</td>';

            $.each(data.data,function(I,V){
                amStr+='<td><ul>';
                pmStr+='<td><ul>';
                $.each(V,function(s,w){
                    for(var j=0;j<w.length;j++){
                        var pubStr= '<li class="'+statusArr[w[j].status.key]+'" data-doctorId="'+w[j].doctor_id+'" data-am="'+s+'" data-time="'+I+'">' +
                            '<span class="text" title="'+w[j].name+'">'+w[j].room+' '+w[j].name+'</span>' +
                            '<span class="status">'+w[j].status.value+'</span></li>';
                        if(s == 0){
                            pmStr+=pubStr;
                        }else if(s == 1){
                            amStr+=pubStr;
                        }
                    }
                });
                amStr+='</ul></td>';
                pmStr+='</ul></td>';
            });
            amStr+='</tr>';
            pmStr+='</tr>';
            str+=amStr+pmStr+'</tbody>';
            $('#adminStatisticsTable').html(str);
        }else if(data.code === 40100){
            aClick();
        }else {
            alertModel('','获取失败，'+data.message);
        }
    }, function(){ });
}
// 根据查看日期 获取医生出诊情况统计  end


//管理员统计 查看预约信息  start
$('#statistical').delegate('.adminStatistics-table tbody li','click',function(){
    if(!($(this).hasClass('stop'))) {
        $('#adminViewReservation').modal('show');
        var doctor_id=$(this).attr('data-doctorId');
        var am=$(this).attr('data-am');
        var date=$(this).attr('data-time');
        var result={name:'',phone:'',times:[date,date],status:'2',doctor_id:doctor_id,am:am,page:1};
        getModalReservationMessage(result);
    }
});
//管理员统计 查看预约信息  end


//***************************管理员操作页面  end***************************



//图表  start
function statisticalChart(chartId,xAxisData,seriesData) {
    Highcharts.chart(chartId, {
        credits:{
            enabled: false //不显示LOGO
        },
        chart: {
            type: 'column'
        },
        title: {
            text: ''  //上面标题
        },
        legend: {
            enabled: false    //下面中间标题
        },
        subtitle: {
            text: ''  //上面小标题
        },
        xAxis: {
            categories: xAxisData,
            lineColor: '#1ab394', //横轴颜色
            lineWidth: 2,
            tickColor: '#1ab394', //刻度颜色
            tickWidth: 2
        },
        yAxis: {
            labels: {
                x: -15
            },
            title: {
                text: '人数／人',
                style:{
                    color:'#1ab394'
                }
            },
            allowDecimals:false, //是否允许纵轴有小鼠
            lineColor: '#1ab394', //纵轴颜色
            lineWidth: 2
        },
        series: [{
            name: '人数',
            data: seriesData,
            color: '#1ab394',
            maxPointWidth: 50    //柱子最大宽度
        }],
        responsive: {
            rules: [{
                condition: {
                    maxWidth: 100
                },
                // Make the labels less space demanding on mobile
                chartOptions: {
                    xAxis: {
                        labels: {
                            formatter: function () {
                                return this.value.replace('月', '')
                            }
                        }
                    },
                    yAxis: {
                        labels: {
                            align: 'left',
                            x: 100,
                            y: -2
                        },
                        title: {
                            text: ''
                        }
                    }
                }
            }]
        }
    });
};
//图表  end

//统计  查看预约信息弹出框  获取数据方法  和  字符串拼接  start
function getModalReservationMessage(result){
    $.get(RESERVATIONCENTERURL.reservationList.index,result,function(data){
        if(data.code === 10000){
            $('#adminViewReservation .modal-body .tableArea').html(reservationMessageStr(data.data));
            // 页码
            if(data.meta.pagination.total_pages>1){
                page('listPager',data.meta,getModalReservationMessage,result);
            }else{
                $('#messageModalPager').html('');
            }
        }else if(data.code === 40100){
            aClick();
        }
        else if(data.code === '40001'){
            $('#adminViewReservation .modal-body .tableArea').html(reservationMessageStr(data.data));
            // 页码
            $('#messageModalPager').html('');
        }
    });
}
function reservationMessageStr(data){
    var str='<table class="table-bordered table-hover text-center reservationCenter-table modalViewReservation-table"><thead><tr><th>序号</th><th>姓名</th><th>性别</th><th>年龄</th><th>手机号</th><th>预约时间</th></tr></thead><tbody>';
    if(data.length){
        $.each(data,function(i,v){
            str+='<tr><td>'+ (i+1) +'</td><td>'+ v.patient.data.name+'</td>' +
                '<td>'+v.patient.data.sex.value+'</td><td>'+v.patient.data.age+'</td>' +
                '<td>'+v.patient.data.phone+'</td>' +
                '<td>'+v.time+' '+v.section+'</td></tr>';
        });
    }else {
        str+='<tr><td colspan="6">暂无数据</td></tr>';
    }
    str+='</tbody></table>';
    return str;
};
//统计  查看预约信息弹出框  获取数据方法  和  字符串拼接  end